// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ManifestTests.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Test.Robotics.Runtime
{
    using System;
    using System.IO;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Runtime.Serialization;
    using System.Threading;
    using Microsoft.Robotics.IO;
    using Microsoft.Robotics.Runtime;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Validates Manifest read/write functionality
    /// </summary>
    [TestClass]
    public class ManifestTests
    {
        /// <summary>
        /// The name of the manifest directory used by tests
        /// </summary>
        private static readonly string TestManifestDirectory = "TestManifests";

        /// <summary>
        /// Recreates the test directory. 
        /// </summary>
        [TestInitialize]
        public void Initialize()
        {
            if (Directory.Exists(TestManifestDirectory))
            {
                Directory.Delete(TestManifestDirectory, true);
            }

            Directory.CreateDirectory(TestManifestDirectory);
        }

        /// <summary>
        /// Removes the test directory. 
        /// </summary>
        [TestCleanup]
        public void Cleanup()
        {
            if (Directory.Exists(TestManifestDirectory))
            {
                Directory.Delete(TestManifestDirectory, true);
            }
        }

        /// <summary>
        /// Validates that the AgentLocator can load a manifest into a collection of agents.
        /// </summary>
        [TestMethod]
        [Priority(0)]
        [TestCategory("Unit")]
        public void AgentLocatorLoadFromManifest()
        {
            Manifest manifest = new Manifest();
            TimerAgent timer = new TimerAgent("Timer", 15);
            manifest.Agents.Add(timer);
            SimpleSubscriberAgent subscriber = new SimpleSubscriberAgent("Subscriber", timer);
            manifest.Agents.Add(subscriber);
            AgentLocator locator = new AgentLocator();
            locator.LoadFrom(manifest);

            // check that the expected agent was deserialized
            IAgentHost agent = locator.GetAgent("Subscriber");

            // no exception indicates success
        }

        /// <summary>
        /// Validates that the AgentLocator can load a file into a collection of agents.
        /// </summary>
        [TestMethod]
        [Priority(0)]
        [TestCategory("Unit")]
        public void AgentLocatorLoadFromManifestFile()
        {
            Manifest manifest = new Manifest();
            TimerAgent timer = new TimerAgent("Timer", 15);
            manifest.Agents.Add(timer);
            SimpleSubscriberAgent subscriber = new SimpleSubscriberAgent("Subscriber", timer);
            manifest.Agents.Add(subscriber);
            string fileName = Path.Combine(TestManifestDirectory, "AgentLocatorLoadFromManifestFile.xml");
            Serializer.Save(fileName, manifest, manifest.GetKnownTypes());

            AgentLocator locator = new AgentLocator();
            locator.LoadFrom(fileName);

            // check that the expected agent was deserialized
            IAgentHost agent = locator.GetAgent("Subscriber");

            // no exception indicates success
        }

        /// <summary>
        /// Validates that the AgentLocator can load a file containing generic agents.
        /// </summary>
        [TestMethod]
        [Priority(0)]
        [TestCategory("Unit")]
        public void AgentLocatorLoadFromManifestFileWithGenerics()
        {
            Manifest manifest = new Manifest();
            GenericAgent<SimpleAgentMessage<int>> testAgent = new GenericAgent<SimpleAgentMessage<int>>();
            manifest.Agents.Add(testAgent);
            string fileName = Path.Combine(TestManifestDirectory, "AgentLocatorLoadFromManifestFileWithGenerics.xml");
            Serializer.Save(fileName, manifest, manifest.GetKnownTypes());

            AgentLocator locator = new AgentLocator();
            locator.LoadFrom(fileName);

            // no exception indicates success
        }
    }
}
